<script setup>
import { h, onMounted, ref, resolveDirective, withDirectives } from 'vue'
import { NButton, NForm, NFormItem, NInput, NInputNumber, NPopconfirm, NTreeSelect } from 'naive-ui'

import CommonPage from '@/components/page/CommonPage.vue'
import QueryBarItem from '@/components/query-bar/QueryBarItem.vue'
import CrudModal from '@/components/table/CrudModal.vue'
import CrudTable from '@/components/table/CrudTable.vue'
import TheIcon from '@/components/icon/TheIcon.vue'

import { renderIcon } from '@/utils'
import { useCRUD } from '@/composables'
// import { loginTypeMap, loginTypeOptions } from '@/constant/data'
import api from '@/api'

defineOptions({ name: '会员管理' })

const $table = ref(null)
const queryItems = ref({})
const vPermission = resolveDirective('permission')

const {
  modalVisible,
  modalTitle,
  modalLoading,
  handleSave,
  modalForm,
  modalFormRef,
  handleEdit,
  handleDelete,
  handleAdd,
} = useCRUD({
  name: '会员等级',
  initForm: { order: 0 },
  doCreate: api.createLever,
  doUpdate: api.updateLever,
  doDelete: api.deleteLever,
  refresh: () => $table.value?.handlerefresh(),
})

const deptOption = ref([])
const isDisabled = ref(false)

onMounted(() => {
  $table.value?.handleSearch()
  // api.getCategory().then((res) => (deptOption.value = res.data))
})

const deptRules = {
  name: [
    {
      required: true,
      message: '请输入名称',
      trigger: ['input', 'blur', 'change'],
    },
  ],
  value: [
    {
      required: true,
      message: '请输入等级值',
      trigger: ['input', 'blur', 'change'],
      type: 'number',
    },
  ],level_integral: [
    {
      required: true,
      message: '请输入成长积分',
      trigger: ['input', 'blur', 'change'],
      type: 'number',
    },
  ],
}

async function addDepts() {
  isDisabled.value = false
  handleAdd()
}

const columns = [
  {
    title: 'ID',
    key: 'id',
    width: 60,
    align: 'center',
    ellipsis: { tooltip: true },
  },
  {
    title: '会员等级名称',
    key: 'name',
    width: 'auto',
    align: 'center',
    ellipsis: { tooltip: true },
  },
  {
    title: '会员等级值',
    key: 'value',
    width: 'auto',
    align: 'center',
    ellipsis: { tooltip: true },
  },
  {
    title: '升级积分',
    key: 'level_integral',
    width: 'auto',
    align: 'center',
    ellipsis: { tooltip: true },
  },
  {
    title: '操作',
    key: 'actions',
    width: 'auto',
    align: 'center',
    fixed: 'right',
    render(row) {
      return [
        withDirectives(
          h(
            NButton,
            {
              size: 'small',
              type: 'primary',
              style: 'margin-right: 8px;',
              onClick: () => {
                console.log('row', row.parent_id)
                if (row.parent_id === 0) {
                  isDisabled.value = true
                } else {
                  isDisabled.value = false
                }
                handleEdit(row)
              },
            },
            {
              default: () => '编辑',
              icon: renderIcon('material-symbols:edit', { size: 16 }),
            }
          ),
          [[vPermission, 'post/api/content/category/update']]
        ),
        h(
          NPopconfirm,
          {
            onPositiveClick: () => handleDelete({ data_id: row.id }, false),
            onNegativeClick: () => { },
          },
          {
            trigger: () =>
              withDirectives(
                h(
                  NButton,
                  {
                    size: 'small',
                    type: 'error',
                  },
                  {
                    default: () => '删除',
                    icon: renderIcon('material-symbols:delete-outline', { size: 16 }),
                  }
                ),
                [[vPermission, 'delete/api/content/category/delete']]
              ),
            default: () => h('div', {}, '确定删除该分类吗?'),
          }
        ),
      ]
    },
  },
]
</script>

<template>
  <!-- 业务页面 -->
  <CommonPage show-footer title="会员等级列表">
    <template #action>
      <div>
        <NButton v-permission="'post/api/content/category/create'" class="float-right mr-15" type="primary"
          @click="addDepts">
          <TheIcon icon="material-symbols:add" :size="18" class="mr-5" />新建会员等级
        </NButton>
      </div>
    </template>
    <!-- 表格 -->
    <CrudTable ref="$table" v-model:query-items="queryItems" :columns="columns" :get-data="api.getLever">
      <template #queryBar>
        <QueryBarItem label="会员等级" :label-width="80">
          <NInput v-model:value="queryItems.name" clearable type="text" placeholder="请输入会员等级名称"
            @keypress.enter="$table?.handleSearch()" />
        </QueryBarItem>
      </template>
    </CrudTable>

    <!-- 新增/编辑 弹窗 -->
    <CrudModal v-model:visible="modalVisible" :title="modalTitle" :loading="modalLoading" @save="handleSave">
      <NForm ref="modalFormRef" label-placement="left" label-align="left" :label-width="80" :model="modalForm"
        :rules="deptRules" :validate-on-rule-change="true">
        <NFormItem label="等级名称" path="name">
          <NInput v-model:value="modalForm.name" clearable placeholder="请输入父级会员等级名称" />
        </NFormItem>
        <NFormItem label="等级值" path="value">
          <NInputNumber v-model:value="modalForm.value" :min="1" />
        </NFormItem>
        <NFormItem label="升级积分" path="level_integral">
          <NInputNumber v-model:value="modalForm.level_integral" :min="0" />
        </NFormItem>
      </NForm>
    </CrudModal>
  </CommonPage>
</template>
